package com.shiyi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author wangzhanshuai
 * @date 2021/1/21 5:58 下午
 * @since 1.0
 */
@Controller
@RequestMapping("/request")
public class RequestController {

    /**
     * 在发送请求时，如果有参数，那么要求方法中的参数名称需要一致，如果不一致，那么可以使用@RequestParam注解进行指定
     *      注解参数解释：
     *          value：指定的参数名称;
     *          required：参数是否是必须的，默认值为true，如果有指定defaultValue，那么该值也会为false;
     *          defaultValue：如果没有该参数，该参数的默认值;
     * @param name 用户名称
     * @return success.jsp
     */
    @RequestMapping("/testRequestParams")
    public String testRequestParams(@RequestParam(value = "username", required = false, defaultValue = "zhang san") String name) {
        System.out.println(name);
        return "success";
    }

    /**
     * @RequestHeader：获取请求头上的参数
     * @param userAgent
     * @return
     */
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader("User-Agent") String userAgent) {
        System.out.println(userAgent);
        return "success";
    }

    /**
     * @CookieValue：获取cookie上的参数
     * 参数与@RequestParam一致
     * @param jsessionId
     * @return
     */
    @RequestMapping("/testCookie")
    public String testCookie(@CookieValue("JSESSIONID") String jsessionId) {
        System.out.println(jsessionId);
        return "success";
    }

    /**
     * SpringMVC也可以在参数上使用原生的Servlet API
     *
     *  HttpSession
     *  HttpServletRequest
     *  HttpServletResponse
     *
     *  java.security.Principal 安全协议相关
     *  Locale：国际化相关的区域信息对象
     *  InputStream:
     *      ServletInputStream inputStream = request.getInputStream();
     *  OutputStream:
     *      ServletOutputStream outputStream = response.getOutputStream();
     *  Reader:
     *      BufferedReader reader = request.getReader();
     *  Writer:
     *      PrintWriter writer = response.getWriter();
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        return "success";
    }
}

